package com.small.base.util;

import com.small.base.constants.NumCommon;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.UUID;

/**
 * StringUtil
 *
 * @author miajio
 */
public class StringUtil {

    /**
     * uuid
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * encodeBase64
     * @param var
     * @return
     */
    public static String encodeBase64(byte[] var) {
        return Base64.getEncoder().encodeToString(var);
    }

    /**
     * decodeBase64
     * @param var
     * @return
     */
    public static byte[] decodeBase64(String var) {
        return Base64.getDecoder().decode(var);
    }

    /**
     * md5
     * @param var
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String md5(String var) throws NoSuchAlgorithmException {
        byte [] secretBytes = MessageDigest.getInstance("MD5").digest(var.getBytes());
        StringBuilder md5code = new StringBuilder(new BigInteger(NumCommon.INT_1, secretBytes).toString(NumCommon.INT_16));
        for (int i = NumCommon.INT_0; i < NumCommon.INT_32 - md5code.length(); i++) {
            md5code.insert(NumCommon.INT_0, "0");
        }
        return md5code.toString();
    }

    /**
     * isEmpty
     * @param var
     * @return
     */
    public static boolean isEmpty(String var) {
        return null == var || var.replaceAll(" ","").isEmpty();
    }

    /**
     * isNotEmpty
     * @param var
     * @return
     */
    public static boolean isNotEmpty(String var) {
        return null != var && !var.replaceAll(" ","").isEmpty();
    }

}
